﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WPFExcelReport.Helpers;
using System.Collections;
using System.Collections.Generic;
namespace WPFExcelReport
{
    public partial class TCPWB02 : UIWBBST
    {
        public string FROMDATE = "", ENDDATE = "",  ARBE = "";
        #region ==WSART===========
        public static readonly DependencyProperty WSARTProperty = DependencyProperty.Register("WSART",
            typeof(string), typeof(TCPWB02),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnWSARTChanged));
        public string WSART
        {
            get
            {
                return (string)GetValue(WSARTProperty);
            }
            set
            {
                SetValue(WSARTProperty, value); 
            }
        }
        private static void OnWSARTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TCPWB02;
            if (src != null)
            {
                src.WSART = (string)e.NewValue;
            }
        }
        #endregion

        #region ==dwWBH===========
        public static readonly DependencyProperty dwGABProperty = DependencyProperty.Register("dwGAB",
            typeof(DataRow), typeof(TCPWB02),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwGABChanged));
        public DataRow dwGAB
        {
            get
            {
                return (DataRow)GetValue(dwGABProperty);
            }
            set
            {
                SetValue(dwGABProperty, value);
            }
        }
        private static void OndwGABChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TCPWB02;
            if (src != null)
            {
                src.dwGAB = (DataRow)e.NewValue;
            }
        }
        #endregion 

        ITXEXRPT ITXEXRPT;
        public TCPWB02()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWBBST();
            this.DataContext = MV; 
            InitializeComponent(); 
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            base.Window_Loaded(sender, e);
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            //这个你今天删除了。这里就是捕捉当前记录的地方
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                WSART = "";
                return;//如果是空记录，则对WSART赋予空值，并返回
            }
            else
            {
                WSART = dwMaster["WBBART"].ToString() + dwMaster["WBBVAR"].ToString() + dwMaster["WBBFARBE"].ToString();
                //便于实现根据当前布卷--库存的品种--，查销售单
                //WSART="{Binding WSART,ElementName=cWAB}"
            }
        }
        private void txtART_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                this.Focus();
                QueryCommand(null, null);
            }


        }
        /// <summary>
        ///  根据品种，色号进行模糊查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void QueryCommand(object sender, System.Windows.RoutedEventArgs e)
        {

            DataSet Result;
            DataSet Return = new DataSet();
            DataRow dwData = null;  
            //FLFD = Helpers.Helper.Right("000000"+txtFLFD.Text.ToString().Trim(),6);
            ARBE = txtARBE.Text.ToString().Trim();
            if (FROMDATE == "")
            {
                FROMDATE = "20000000";
            }
            if (ENDDATE == "")
            {
                ENDDATE = "99999999";
            }
            dwData =TBMain.NewRow();
            dwData["WBBART"] = ART;
            dwData["WBBFARBE"] = ARBE;
            dwData["WBBLO"] = LO;
            dwData["WBBLB"] = LB;
            try
            {
                
                Result = TMV.QueryWBBST(dwData, 16);
                MergeData(Result, true, true);//查询结果在TBMain表，位于Setprod. WBBST，vwMaster是这个内容的视图，也就是这个表。不过，当鼠标定位等的时候，vwMaster会捕捉到当前记录，

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        private void FROM_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                FROMDATE = "";
                FROMDATE = Helpers.Helper.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }



        private void PrintCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (ITXEXRPT == null)
                {
                    ITXEXRPT = new ITXEXRPT();
                    ITXEXRPT.Root = "C:\\IntexReport";
                    ITXEXRPT.strCompany = app.company;
                    ITXEXRPT.PrintType = "OpenExcel";
                }
                ITXEXRPT.dsSource = new DataSet();
                ITXEXRPT.dsSource.Merge(MV.SetProd);
                //ITXEXRPT.dsSource.Merge(Se.Env);

                ITXEXRPT.DocType = "JLTEK02";

                ITXEXRPT.strExcelFile = @"JLT\JLTEK02.xls";
                string filename = ITXEXRPT.WriteExcelReport();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void END_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                ENDDATE = "";
                ENDDATE = Helpers.Helper.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        private void PrintLBCommand(object sender, RoutedEventArgs e)
        {
            if (ITXEXRPT == null)
            {
                ITXEXRPT = new WPFExcelReport.ITXEXRPT();
                ITXEXRPT.Root = "C:\\IntexReport";
                ITXEXRPT.strCompany = app.company;
                ITXEXRPT.PrintType = "OpenExcel";
            }
            ITXEXRPT.dsSource = new DataSet();
            bool IsFound = false;
            if (TBMain.Rows.Count == 0)
                return;
            DataTable tb = TBMain.Clone();
            foreach (var item in dlgMaster.SelectedItems)
            {
                if (!item.GetType().ToString().Contains("DataRowView")) continue;

                DataRow dwItem = ((System.Data.DataRowView)item).Row;
                DataRow _dwWB = tb.Rows.Add();
                _dwWB.ItemArray = dwItem.ItemArray;
            }
            switch (app.company)
            {
                default:
                case "JLT":
                    ITXEXRPT.dsSource.Merge(tb);
                    ITXEXRPT.DocType = "JLTWBD10IT";
                    foreach (DataRow row in tb.Rows)
                    {
                        string PACKNR = row["WBBPACKNR"].ToString();
                        if (PACKNR == "0000")
                            continue;
                        string art = row["WBBART"].ToString().Trim();
                        string dr = @"C:\Program Files\mabu\templates";
                        string filename = "JLB" + art.Substring(3, 4) + ".xls";
                        if (!System.IO.Directory.Exists(dr))
                        {
                            IsFound = false;
                        }
                        else
                        {
                            string[] files = System.IO.Directory.GetFiles(dr);
                            foreach (string file in files)
                            {
                                if (!file.Contains(filename)) continue; IsFound = true; ITXEXRPT.strExcelFile = file; break;
                            }
                            if (!IsFound)
                            {
                                filename = "coillabel" + art.Substring(3, 4) + ".xls";
                                foreach (string file in files)
                                {
                                    if (!file.Contains(filename)) continue; IsFound = true; ITXEXRPT.strExcelFile = file; break;
                                }
                            }
                        }
                    }
                    if (IsFound) ITXEXRPT.WriteExcelReport();
                    break;
            }
        }

        private void ExcelCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (ITXEXRPT == null)
                {
                    ITXEXRPT = new ITXEXRPT();
                    ITXEXRPT.Root = "C:\\IntexReport";
                    ITXEXRPT.strCompany = app.company;
                    ITXEXRPT.PrintType = "OpenExcel";
                }
                ITXEXRPT.dsSource = new DataSet();
                ITXEXRPT.dsSource.Merge(MV.SetProd);
                //ITXEXRPT.dsSource.Merge(Se.Env);

                ITXEXRPT.DocType = "JLTCPWB02";

                ITXEXRPT.strExcelFile = @"JLT\JLTCPWB02.xls";
                string filename = ITXEXRPT.WriteExcelReport();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

    }
}
